import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	http
} from "../common/http.js"

export const store = new Vuex.Store({
	state: {
		userInfo: uni.getStorageSync('userInfo') || {},
		token: uni.getStorageSync('token') || '',
		history: uni.getStorageSync('history') || [],
		site: uni.getStorageSync('site') ||'',
	},
	mutations: {
		getHistory(state, key) {
			if (key && !state.history.includes(key)) {
				state.history.push(key)
				uni.setStorageSync('history', state.history)
			}
		},
		delHistory(state) {
			state.history = []
			uni.setStorageSync('history', state.history)
		},
		getCity(state, city) {
			state.city = city
			uni.setStorageSync('city', state.city)
		},
		getSite(state, site) {
			state.site = site
			uni.setStorageSync('site', state.site)
		},
		getUserInfo(state, info) {
			state.userInfo = info
			state.token = state.userInfo.token
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
		},
		exitLogin(state) {
			state.userInfo = {}
			state.token = ''
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('token', state.userInfo.token)
		},
	},
	getters: {},
	actions: {
		updateUserInfo({
			commit
		}) {
			http('/api/user/userinfo', "POST").then(data => {
				commit('getUserInfo', data)
			})
		}
	}
})
